﻿/*
**签约报备页面
*/
Chenxin.UI.ContractArchivePage = Chenxin.extend(Chenxin.UI.FormCommon, {
    actionUrl: "/service/logic/ContractArchive.ashx",
    type: 'numal',
    id: "divContext",
    beforeFitData: function(data) {
        var _this = this;
        if (data.BargainType == '面签' || data.BargainType == undefined) {
            $("#tr_person").hide();
            _this.findByName('WorkPersonId').required = false;
        } else {
            $("#tr_person").show();
            _this.findByName('WorkPersonId').required = true;
        }
    },
    createComboList: function() {
        var _this = this;
        /****************************报备信息*************************/
        //报备部门
        var comboDepartId = new Chenxin.UI.StructCombo({
            param: { op: 'getcontroldeparts' },
            containerId: '#DepartId',
            name: 'DepartId',
            inModelType: 'all',
            txtModelName: 'DepartName',
            required: true,
            tbxWidth: 120,
            colNum: 4,
            hasPage: true,
            pageSize: 20,
            hasSearch: true,
            colWidth: 80,
            onSelected: function() {
                if (this.getValue() != this.defVal) {

                    comboEmployeeId.loadUser(this.getValue());
                }
            },
            afterSetValue: function(newVal, oldVal) {
                if (newVal != "请选择") {
                    comboEmployeeId.loadUser(newVal);
                }
            }
        });
        //报备人员
        var comboEmployeeId = new Chenxin.UI.RichModelTreeCombo({
            textBoxId: 'txtEmployeeId',
            containerId: '#txtEmployeeId',
            inModelType: 'all',
            txtModelName: 'Employee',
            required: true,
            singleSelect: true,
            name: 'EmployeeId'
        });
        //贷款情况
        var comboLoanInfo = new Chenxin.UI.Combo({
            containerId: '#LoanInfo',
            name: 'LoanInfo',
            required: true,
            tbxWidth: 120,
            colWidth: 80,
            items: comboCommon.ArchiveLoanInfoType
        });
        //合同归属（城市）
        var comboBargainCityId = new Chenxin.UI.StructCombo({
            param: { op: 'getcity' },
            containerId: '#BargainCityId',
            name: 'BargainCityId',
            tbxWidth: 120,
            inModelType: 'all',
            txtModelName: 'BargainCityName',
            onSelected: function() {
                if (this.getValue() == this.defVal) {
                    comboBargainCompanyId.clearItems();
                } else {
                    comboBargainCompanyId.param.fatherid = this.getValue();
                    comboBargainCompanyId.reload();
                }
            },
            afterSetValue: function(newVal, oldVal) {
                if (newVal != "请选择") {
                    comboBargainCompanyId.param.fatherid = newVal;
                    comboBargainCompanyId.reload(_this.model.BargainCompanyId);
                }
            }
        });
        //合同归属（公司）
        var comboBargainCompanyId = new Chenxin.UI.StructCombo({
            param: { op: 'getcompany' },
            containerId: '#BargainCompanyId',
            name: 'BargainCompanyId',
            inModelType: 'all',
            txtModelName: 'BargainCompanyName',
            tbxWidth: 120,
            autoLoad: false,
            required: true
        });
        //签约地点（城市）
        var comboWorkCityId = new Chenxin.UI.StructCombo({
            param: { op: 'getcity' },
            containerId: '#WorkCityId',
            name: 'WorkCityId',
            inModelType: 'all',
            txtModelName: 'WorkCityName',
            tbxWidth: 120,
            onSelected: function() {
                if (this.getValue() == this.defVal) {
                    comboBargainCompanyId.clearItems();
                } else {
                    comboWorkCompanyId.param.fatherid = this.getValue();
                    comboWorkCompanyId.reload();
                }
                // comboWorkCompanyId.clearItems();
                comboWorkDepartId.clearItems();
                comboWorkPersonId.clearItems();
            },
            afterSetValue: function(newVal, oldVal) {
                if (newVal != "请选择") {
                    comboWorkCompanyId.param.fatherid = newVal;
                    comboWorkCompanyId.reload(_this.model.WorkCompanyId);
                }
            }
        });
        //签约地点（公司）
        var comboWorkCompanyId = new Chenxin.UI.StructCombo({
            param: { op: 'getcompany' },
            containerId: '#WorkCompanyId',
            name: 'WorkCompanyId',
            inModelType: 'all',
            txtModelName: 'WorkCompanyName',
            tbxWidth: 120,
            autoLoad: false,
            onSelected: function() {
                if (this.getValue() == this.defVal) {
                    comboWorkDepartId.clearItems();
                } else {
                    if ($("input[name='BargainType']:checked").val() == "面签") {
                        comboWorkDepartId.param.op = "getbargaindeparts";
                    } else {
                        comboWorkDepartId.param.op = "getsigndeparts";
                    }
                    comboWorkDepartId.param.fatherid = this.getValue();
                    comboWorkDepartId.reload();
                }
                //comboWorkDepartId.clearItems();
                comboWorkPersonId.clearItems();
            },
            afterSetValue: function(newVal, oldVal) {
                if (newVal != "请选择") {
                    if ($("input[name='BargainType']:checked").val() == "面签") {
                        comboWorkDepartId.param.op = "getbargaindeparts";
                    } else {
                        comboWorkDepartId.param.op = "getsigndeparts";
                    }
                    comboWorkDepartId.param.fatherid = newVal;
                    comboWorkDepartId.reload(_this.model.WorkDepartId);
                }
            }
        });
        //签约人
        var comboWorkPersonId = new Chenxin.UI.StructCombo({
            param: { op: 'getemployee' },
            containerId: '#WorkPersonId',
            inModelType: 'all',
            txtModelName: 'WorkPersonName',
            autoLoad: false,
            required: false,
            tbxWidth: 120,
            colNum: 3,
            hasPage: true,
            pageSize: 15,
            hasSearch: true,
            colWidth: 100
        });
        //签约地点（部门）
        var comboWorkDepartId = new Chenxin.UI.StructCombo({
            param: { op: 'getsigndeparts' },
            containerId: '#WorkDepartId',
            inModelType: 'all',
            txtModelName: 'WorkDepartName',
            tbxWidth: 120,
            colWidth: 90,
            colNum: 3,
            autoLoad: false,
            required: true,
            hasPage: true,
            hasSearch: true,
            pageSize: 8,
            onSelected: function() {
                if (this.getValue() == this.defVal) {
                    comboWorkPersonId.clearItems();
                } else {
                    if ($("input[name='BargainType']:checked").val() == "面签") {
                        //comboWorkPersonId.param.op = "getcenteruser";
                    } else {
                        comboWorkPersonId.param.op = "getemployee";
                        comboWorkPersonId.param.fatherid = this.getValue();
                        comboWorkPersonId.reload();
                    }
                }
            },
            afterSetValue: function(newVal, oldVal) {
                if (newVal != "请选择") {
                    if ($("input[name='BargainType']:checked").val() == "面签") {
                        //comboWorkPersonId.param.op = "getcenteruser";
                    } else {
                        comboWorkPersonId.param.op = "getemployee";
                        comboWorkPersonId.param.fatherid = newVal;
                        comboWorkPersonId.reload(_this.model.WorkPersonId);
                    }
                }
            }
        });


        /****************************房源信息*************************/
        //竣工日期
        var comboCompleteYear = new Chenxin.UI.BuildingYearCombo({
            containerId: '#CompleteYear',
            tbxWidth: 120,
            required: true
        });

        //建筑类型
        var comboBuildingType = new Chenxin.UI.ParameterCombo({
            containerId: '#BuildingType',
            key: 'Building.Type',
            inModelType: 'txt',
            tbxWidth: 120,
            required: true
        });
        //物业用途
        var comboHouseUse = new Chenxin.UI.ParameterCombo({
            containerId: '#HouseUse',
            key: 'House.Use',
            inModelType: 'txt',
            tbxWidth: 120,
            required: true
        });

        /****************************交易信息*************************/
        //合同总价单位
        var comboUnit = new Chenxin.UI.Combo({
            containerId: '#Unit',
            name: 'Unit',
            addPleaseSelected: false,
            tbxWidth: 40,
            items: comboCommon.PriceUnit
        });
        return [comboDepartId, comboEmployeeId, comboLoanInfo, comboBargainCityId, comboBargainCompanyId, comboWorkCityId, comboWorkCompanyId, comboWorkDepartId, comboWorkPersonId, comboCompleteYear, comboBuildingType, comboHouseUse, comboUnit];
    },
    initFormValidator: function() {
        var objFrm = this;
        //签约时间
        $("#txtBargainTime").formValidator({
            validatorgroup: this.id,
            onshow: "请输入签约时间！",
            onfocus: "签约时间格式：YYYY-MM-DD HH:mm",
            oncorrect: " "
        }).inputValidator({
            min: 1,
            empty: {
                leftempty: false,
                rightempty: false,
                emptyerror: "签约时间不能为空！"
            },
            onerror: "签约时间不能为空！"
        });
        //过户日期
        $("#txtTransferDate").formValidator({
            validatorgroup: this.id,
            onshow: "请输入过户日期！",
            onfocus: "过户日期格式：YYYY-MM-DD",
            oncorrect: " "
        }).inputValidator({
            min: 1,
            empty: {
                leftempty: false,
                rightempty: false,
                emptyerror: "过户日期不能为空！"
            },
            onerror: "过户日期不能为空！"
        });
        //签贷款时间
        $("#txtLoanTime").formValidator({
            validatorgroup: this.id,
            onshow: "请输入签贷款时间！",
            onfocus: "签贷款时间格式：YYYY-MM-DD HH:mm",
            oncorrect: " "
        }).functionValidator({
            fun: function(val) {
                if (objFrm.comboList[2].getValue() == "一次性付款") {
                    return true;
                } else {
                    if (val == "") {
                        return false;
                    } else {
                        return true;
                    }
                }
            },
            onerror: "签贷款时间不能为空！"
        });
        //联系电话
        $("#txtMobile").formValidator({
            validatorgroup: this.id,
            onshow: "请输入联系电话！",
            onfocus: "联系电话为必输项",
            oncorrect: " "
        }).inputValidator({
            min: 1,
            empty: {
                leftempty: false,
                rightempty: false,
                emptyerror: "联系电话不能为空！"
            },
            onerror: "联系电话不能为空！"
        });
        //房源地址
        $("#txtNewHouseAddress").formValidator({
            validatorgroup: this.id,
            onshow: "请输入房源地址！",
            onfocus: "房源地址为必输项",
            oncorrect: " "
        }).inputValidator({
            min: 1,
            empty: {
                leftempty: false,
                rightempty: false,
                emptyerror: "房源地址不能为空！"
            },
            onerror: "房源地址不能为空！"
        });
        //建筑面积
        $("#txtBuildingArea").formValidator({
            validatorgroup: this.id,
            onshow: "请输入建筑面积！",
            onfocus: "建筑面积为必输项",
            oncorrect: " "
        }).inputValidator({
            min: 1,
            empty: {
                leftempty: false,
                rightempty: false,
                emptyerror: "建筑面积不能为空！"
            },
            onerror: "建筑面积不能为空！"
        });
        //总楼层
        $("#txtTotal_Floor").formValidator({
            validatorgroup: this.id,
            onshow: "请输入总楼层！",
            onfocus: "总楼层为必输项",
            oncorrect: " "
        }).inputValidator({
            min: 1,
            empty: {
                leftempty: false,
                rightempty: false,
                emptyerror: "总楼层不能为空！"
            },
            onerror: "总楼层不能为空！"
        });
        //房屋楼层
        $("#txtCur_Floor").formValidator({
            validatorgroup: this.id,
            onshow: "请输入房屋楼层！",
            onfocus: "房屋楼层为必输项",
            oncorrect: " "
        }).inputValidator({
            min: 1,
            empty: {
                leftempty: false,
                rightempty: false,
                emptyerror: "房屋楼层不能为空！"
            },
            onerror: "房屋楼层不能为空！"
        });
        //合同总价
        $("#txtTotalPrice").formValidator({
            validatorgroup: this.id,
            onshow: "请输入合同总价！",
            onfocus: "格式：整数或最多保留2位小数",
            oncorrect: " "
        }).inputValidator({
            min: 1,
            empty: {
                leftempty: false,
                rightempty: false,
                emptyerror: "合同总价不能为空！"
            },
            onerror: "合同总价不能为空！"
        }).regexValidator({
            regexp: "decmal7",
            datatype: "enum",
            onerror: "格式不正确"
        });
        // 补偿款
        $("#txtFitPrice").formValidator({
            validatorgroup: this.id,
            onshow: "请输入补偿款！",
            onfocus: "格式：整数或最多保留2位小数",
            oncorrect: " "
        }).regexValidator({
            regexp: "decmal8",
            datatype: "enum",
            onerror: "格式不正确"
        });
        //维修基金
        /*
        $("#txtRepairFund").formValidator({
        validatorgroup: this.id,
        onshow: "请输入维修基金！",
        onfocus: "格式：整数或最多保留2位小数或空",
        oncorrect: " "
        }).regexValidator({
        regexp: "decmal8",
        datatype: "enum",
        onerror: "格式不正确"
        });*/
        //联系地址（出售方）
        $("#txtVenderAddress").formValidator({
            validatorgroup: this.id,
            onshow: "请输入联系地址！",
            onfocus: "联系地址为必输项",
            oncorrect: " "
        }).inputValidator({
            min: 1,
            empty: {
                leftempty: false,
                rightempty: false,
                emptyerror: "联系地址不能为空！"
            },
            onerror: "联系地址不能为空！"
        });
        //联系电话（出售方）
        $("#txtVenderTel").formValidator({
            validatorgroup: this.id,
            onshow: "请输入联系电话！",
            onfocus: "联系电话为必输项",
            oncorrect: " "
        }).inputValidator({
            min: 1,
            empty: {
                leftempty: false,
                rightempty: false,
                emptyerror: "联系电话不能为空！"
            },
            onerror: "联系电话不能为空！"
        });
        //产权证号
        $("#txtPropertyCode").formValidator({
            validatorgroup: this.id,
            onshow: "请输入产权证号！",
            onfocus: "产权证号为必输项",
            oncorrect: " "
        }).inputValidator({
            min: 1,
            empty: {
                leftempty: false,
                rightempty: false,
                emptyerror: "产权证号不能为空！"
            },
            onerror: "产权证号不能为空！"
        });
        //联系地址（买受方）
        $("#txtBuyerAddress").formValidator({
            validatorgroup: this.id,
            onshow: "请输入联系地址！",
            onfocus: "联系地址为必输项",
            oncorrect: " "
        }).inputValidator({
            min: 1,
            empty: {
                leftempty: false,
                rightempty: false,
                emptyerror: "联系地址不能为空！"
            },
            onerror: "联系地址不能为空！"
        });
        //联系电话（买受方）
        $("#txtBuyerTel").formValidator({
            validatorgroup: this.id,
            onshow: "请输入联系电话！",
            onfocus: "联系电话为必输项",
            oncorrect: " "
        }).inputValidator({
            min: 1,
            empty: {
                leftempty: false,
                rightempty: false,
                emptyerror: "联系电话不能为空！"
            },
            onerror: "联系电话不能为空！"
        });
    },
    initEvent: function() {
        var objFrm = this;

        $('#aReturn').click(function() {
            //window.location.href($("#ctl00_main_hidHouseUrl").val());
            window.history.go(-1);
        });

        $('.x_colexp').collapse({
            getDom: function() {
                return $('#' + this.id.replace('x_btn_col_', 'x_form_'));
            }
        });

        /*************事件：签约时间得到焦点*************/
        $("#txtBargainTime").focus(function() {
            WdatePicker({
                dateFmt: 'yyyy-MM-dd HH:mm',
                oncleared: function() {
                    $(this).blur();
                },
                onpicked: function() {
                    $(this).blur();
                }
            });
        });
        /*************事件：贷款时间得到焦点*************/
        $("#txtLoanTime").focus(function() {
            WdatePicker({
                dateFmt: 'yyyy-MM-dd HH:mm',
                oncleared: function() {
                    $(this).blur();
                },
                onpicked: function() {
                    $(this).blur();
                }
            });
        });
        $("#txtTransferDate").focus(function() {
            WdatePicker({
                dateFmt: 'yyyy-MM-dd',
                oncleared: function() {
                    $(this).blur();
                },
                onpicked: function() {
                    $(this).blur();
                }
            });
        });
        /*************事件：签约方式变更*************/
        $("input[name=BargainType]").click(function() {
            //签约地点重置
            objFrm.comboList[5].setValue(objFrm.comboList[5].defVal);
            objFrm.comboList[6].clearItems();
            objFrm.comboList[7].clearItems();
            objFrm.comboList[8].clearItems();
        });

        /*************事件：签约方式变更*************/
        objFrm.findByName('BargainType').click(function() {
            if (this.value == '面签') {
                $("#tr_person").hide();
                objFrm.findByName('WorkPersonId').required = false;
            } else {
                $("#tr_person").show();
                objFrm.findByName('WorkPersonId').required = true;
            }
        });

        /*************事件：预审客户信息*************/
        $("#aPreliminaryUser").click(function() {
            if (!confirm("导入预审客户信息将覆盖已填写数据，确定要导入吗？")) {
                return false;
            }

            var HouseId = Chenxin.Fn.getQueryString('HouseId');
            if (HouseId == undefined || HouseId == "") {
                HouseId = objFrm.model.HouseId;
            }

            Chenxin.ajax({
                type: "get",
                dataType: "json",
                url: objFrm.actionUrl,
                data: { op: 'getpreliminaryuser', HouseId: HouseId },
                onSuccess: function(data) {
                    objFrm.infoBGrid.render(data.PreBuyerInfo);
                }
            });
        });


        //提交
        $("#aSubmit").click(function() {
            if (objFrm.ContractArcInfo != undefined) {
                objFrm.op = "upd";
            }
            objFrm.save();
        });
    },
    saveSucc: function(data, msg) {

        var strUrl;
        strUrl = '<a title="返回房源管理" href="' + $("#ctl00_main_hidHouseUrl").val() + '">返回房源管理</a>';
        strUrl = strUrl + '<br/><br/>';
        strUrl = strUrl + '<a title="返回签约报备" href="ShowListArchive.aspx">返回签约报备</a>';
        strUrl = strUrl + '<br/><br/>';
        strUrl = strUrl + '<a title="查看该报备" href=ArchiveDetailInfo.aspx?RecordId=' + data + ' >查看该报备</a>';
        Chenxin.Msg.alert.show(strUrl, '下一步', Chenxin.Msg.icon.next, function() {
            window.location.href('ShowListArchive.aspx');
        });
    },
    afterGetFormJson: function(json) {
        //甲方信息
        if (this.infoAGrid.store.rows.length) {
            var rows = [];
            for (var i = 0; i < this.infoAGrid.store.rows.length; i++) {
                var row = {};
                Chenxin.apply(row, this.infoAGrid.store.rows[i]);
                delete row.RecordId;
                if (this.ContractArcInfo != undefined)
                    row.MainId = this.ContractArcInfo.RecordId;
                rows.push(row);
            }
            this.additionalParam.lsVerder = '[' + rows.toString() + ']';
        }
        //乙方信息
        if (this.infoBGrid.store.rows.length) {
            var rows = [];
            for (var i = 0; i < this.infoBGrid.store.rows.length; i++) {
                var row = {};
                Chenxin.apply(row, this.infoBGrid.store.rows[i]);
                delete row.RecordId;
                if (this.ContractArcInfo != undefined)
                    row.MainId = this.ContractArcInfo.RecordId;
                rows.push(row);
            }
            this.additionalParam.lsBuyer = '[' + rows.toString() + ']';
        }
        Chenxin.applyIf(json, this.model);
        json.HouseAddress = json.NewHouseAddress;
        delete json.NewHouseAddress;
        delete json.BOwner;

        if (json.BargainType == '面签') {
            delete json.WorkPersonId;
            delete json.WorkPersonName;
        }
    },
    initData: function() {
        var _this = this;
        var recordId = Chenxin.Fn.getQueryString('RecordId');
        var houseId = Chenxin.Fn.getQueryString('HouseId');
        var model = {};
        if (houseId != undefined && houseId != "") {
            Chenxin.Fn.getModel(houseId || '', this.actionUrl, function(data) {
                this.HouseInfo = data.HouseInfo;
                //Chenxin.apply(model, this.HouseInfo);
                model.BuildingArea = this.HouseInfo.BuildingArea;
                model.Cur_Floor = this.HouseInfo.Cur_Floor;
                model.Total_Floor = this.HouseInfo.Total_Floor;
                model.HouseId = this.HouseInfo.HouseId;
                model.HouseCode = this.HouseInfo.HouseCode;
                model.CompleteYear = this.HouseInfo.CompleteYear;
                model.BuildingType = this.HouseInfo.BuildingType;
                model.HouseUse = this.HouseInfo.HouseUse;
                model.NewHouseAddress = this.HouseInfo.DetailAddress;
                model.Unit = "RMB";
                model.BelongUserName = this.HouseInfo.OprPerson;
                model.BelongUserId = this.HouseInfo.OprPersonId;
                model.BelongUserDepartmentName = this.HouseInfo.OprDepart;
                model.BelongUserDepartmentId = this.HouseInfo.OprDepartId;
                model.BelongUserCityId = this.HouseInfo.CityId;
                model.BelongUserCompanyId = this.HouseInfo.CompanyId;
                model.BelongUserCompanyName = this.HouseInfo.CompanyName;
                this.model = model;
                this.fitData(model);
            }, this, 'gethousedata');
        }
        else {
            Chenxin.Fn.getModel(recordId || '', this.actionUrl, function(data) {
                this.ContractArcInfo = data.ContractArcInfo;
                //签约时间
                if (this.ContractArcInfo.BargainTime != undefined && this.ContractArcInfo.BargainTime.length > 0)
                    this.ContractArcInfo.BargainTime = this.ContractArcInfo.BargainTime.substr(0, 16);
                //签贷款时间
                if (this.ContractArcInfo.LoanTime != undefined && this.ContractArcInfo.LoanTime.length > 0)
                    this.ContractArcInfo.LoanTime = this.ContractArcInfo.LoanTime.substr(0, 16);
                //还贷时间
                if (this.ContractArcInfo.PayLoanDay != undefined && this.ContractArcInfo.PayLoanDay.indexOf(" ") > 0) {
                    this.ContractArcInfo.PayLoanDay = (this.ContractArcInfo.PayLoanDay.split(" "))[0];
                }
                //交房时间
                if (this.ContractArcInfo.GiveHouseTime != undefined && this.ContractArcInfo.GiveHouseTime.indexOf(" ") > 0) {
                    this.ContractArcInfo.GiveHouseTime = (this.ContractArcInfo.GiveHouseTime.split(" "))[0];
                }
                //过户日期
                if (this.ContractArcInfo.TransferDate != undefined && this.ContractArcInfo.TransferDate.indexOf(" ") > 0) {
                    this.ContractArcInfo.TransferDate = (this.ContractArcInfo.TransferDate.split(" "))[0];
                }
                //房源地址
                this.ContractArcInfo.NewHouseAddress = this.ContractArcInfo.HouseAddress;

                Chenxin.apply(model, this.ContractArcInfo);

                model.DepartId = this.ContractArcInfo.DepartId;
                model.EmployeeId = this.ContractArcInfo.EmployeeId;
                model.LoanInfo = this.ContractArcInfo.LoanInfo;
                model.BargainCityId = this.ContractArcInfo.BargainCityId;
                model.BargainCompanyId = this.ContractArcInfo.BargainCompanyId;
                model.WorkCityId = this.ContractArcInfo.WorkCityId;
                model.WorkCompanyId = this.ContractArcInfo.WorkCompanyId;
                model.WorkDepartId = this.ContractArcInfo.WorkDepartId;
                model.WorkPersonId = this.ContractArcInfo.WorkPersonId;
                model.CompleteYear = this.ContractArcInfo.CompleteYear;
                model.BuildingType = this.ContractArcInfo.BuildingType;
                model.HouseUse = this.ContractArcInfo.HouseUse;
                model.Unit = this.ContractArcInfo.Unit;

                this.model = model;
                this.fitData(model);

                this.infoAGrid.render(data.VenderInfo);
                this.infoBGrid.render(data.BuyerInfo);
            }, this, 'getapplydata');

        }
    },
    createGrid: function() {
        this.infoAGrid = new Chenxin.UI.InfoAGrid();
        this.infoBGrid = new Chenxin.UI.InfoBGrid();
    },
    initComponent: function() {
        Chenxin.UI.ContractArchivePage.superclass.initComponent.call(this);
        this.createGrid();
        this.initData();

    }

});

dataKeyList = 'Building.Type,House.Use'; //设置需要获取数据字段数据的KEY,用,分割
getcity = true; //设置需要获取城市下拉框数据

$(document).ready(function() {
    var dom = new Chenxin.UI.ContractArchivePage();
});